﻿using System;
using System.ComponentModel;

namespace Application
{
    public class ProductEditorViewModel : INotifyPropertyChanged
    {
        private readonly int _id;
        private string _name;
        private string _description;
        private double _price;

        public ProductEditorViewModel(int id)
        {
            _id = id;
            _name = String.Empty;
            _description = String.Empty;
            _price = default(double);
        }

        public bool IsValid
        {
            get 
            { 
                return 
                    IsNameValid &&
                    IsDescriptionValid; 
            }
        }

        protected bool IsNameValid
        {
            get { return _name.Trim().Length > 0; }
        }

        protected bool IsDescriptionValid
        {
            get { return _description.ToCharArray().Length > 0; }
        }

        public string Name
        {
            get { return _name; }
            set
            {
                if(value == null) throw new ArgumentNullException("value");

                if(_name != value)
                {
                    var wasValid = IsValid;
                    _name = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Name"));
                    NotifyValid(wasValid);
                }
            }
        }

        public string Description
        {
            get { return _description; }
            set
            {
                if (value == null) throw new ArgumentNullException("value");

                if (_description != value)
                {
                    var wasValid = IsValid;
                    _description = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Description"));
                    NotifyValid(wasValid);
                }
            }
        }

        public double Price
        {
            get { return _price; }
            set
            {
                if (value == 0.00) throw new ArgumentNullException("value");

                if (_price != value)
                {
                    var wasValid = IsValid;
                    _price = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Description"));
                    NotifyValid(wasValid);
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = PropertyChanged;
            if(handler != null)
            {
                handler(this, e);
            }
        }

        private void NotifyValid(bool wasValid)
        {
            if(IsValid != wasValid)
            {
                OnPropertyChanged(new PropertyChangedEventArgs("IsValid"));
            }
        }
    }
}